// pages/video/video.js
var utils = require('../../utils/util.js')
const API = require('../../API/api.js')
var app = getApp()
Page({

   /**
    * 页面的初始数据
    */
   data: {

      videoInfo: {},
      serverUrl: app.serverUrl,
      cover: "cover",
      userLikeVideo: false,
      publisher: {},
      commentsList: [{
         nickname: "杨伟乔",
         timeAgoStr: "测试",
         toNickname: "测试2",
         comment: "pinglun 评论 "
      }
      ]

   },
   videoContext: {},
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
      var me = this;
      var video = JSON.parse(options.video);
      var videostr = JSON.stringify(video);

      me.redirectUrl = '../video/video#video@' + videostr

      console.log(video);

      var cover = "cover"
      if (video.videoHeight > video.videoWidth) {
         cover = "cover"
      } else {
         cover = "contain"
      }
      var me = this;
      me.videoContext = wx.createVideoContext('myVideo');

      me.setData({
         videoInfo: video,
         cover: cover,
      });
      var user = app.getGlobaluserInfo()
      //用户是否点赞当前视频  
      API.isLikeVideo({
         'userId': user.id,
         'videoId': video.id
      }).then(
         res => {
            console.log(res)
            me.setData({
               userLikeVideo: res
            });
         }
      )
   },



   onShow: function () {
      console.log("界面显示")
      this.videoContext.play()
   },
   onHide: function () {
      this.videoContext.stop()
   },


   showSearch: function (params) {
      console.log("搜索框的点击事件")
      //跳转到新的界面 
      wx.navigateTo({
         url: '/pages/search/search',
      })
   },

   upload: function () {
      var me = this;


      var user = app.getGlobaluserInfo()
      if (user == null || user == '' || user == undefined) {
         //去登录界面 
         wx.redirectTo({
            url: '../userLogin/login?redirectUrl=' + me.redirectUrl,
         })
      } else {
         utils.uploadVideo()
      }

   },

   showMine: function () {
      //判断当前用户是否登录 
      var user = app.getGlobaluserInfo()
      if (user == null || user == '' || user == undefined) {
         //去登录界面 
         wx.navigateTo({
            url: '../userLogin/login',
         })
      } else {
         wx.navigateTo({
            url: '../mine/mine',
         })
      }


   },

   showPublisher: function () {
      var me = this;
      var videoInfo = me.data.videoInfo;
      wx.navigateTo({
         url: '../mine/mine?userId=' + videoInfo.userId,
      })
   },

   //点赞或者取消点赞 

   likeVideoOrNot: function () {
      var me = this;
      var serverUrl = app.serverUrl;
      var user = app.getGlobaluserInfo();
      var video = this.data.videoInfo;
      //点赞的点击事件 点赞或者是取消点赞 

      API.like(
         { 'userId': user.id, 'videoId': video.id }
      ).then(
         res => {
            console.log(res)
            me.setData({
               userLikeVideo: res.status == 1
            });

         }
      )
      // wx.request({
      //    url: serverUrl + '/videos/like',
      //    method: 'POST',

      //    header: {
      //       //设置头 请求数据转换为string  或者不要data 直接在url后面拼接 
      //       'content-type': 'application/x-www-form-urlencoded'
      //    },
      //    success: function (res) {
      //       console.log(res)
      //       me.setData({
      //          userLikeVideo: res.data.data.status == 1
      //       });

      //    }

      // })

   },

   showIndex: function () {
      wx.redirectTo({
         url: '../home/home',
      })
   }
})